#ifndef ASTAR_H
#define ASTAR_H

#include "global.h"

#define MAX_NODE_COUNT          (MAX_BLOCK_COUNT * 2 + 1)   // 地图最大列数/行数
#define MAX_BORDER_COUNT        (MAX_NODE_COUNT + 1)        // 地图最大边数
#define MAX_PATH_NODE           (MAX_BLOCK_COUNT * 2)       // 最大可存储路径点数

enum ASTAR_NODE_STATE
{
    ASTAR_NODE_STATE_NONE,
    ASTAR_NODE_STATE_OPEN,
    ASTAR_NODE_STATE_CLOSE,
};

typedef struct ASTAR_NODE_INDEX
{
    uchar x;
    uchar y;
} ASTAR_NODE_INDEX;

typedef struct ASTAR_NODE
{
    uchar x;                        // 块x索引
    uchar y;                        // 块y索引
    uchar wall : 1;                 // 障碍区域, 无法进入
    uchar state : 2;
    uchar cost;                     // the cost of getting from the prev node to the node.
    uchar f_cost;                   // h(n) is a heuristic function that estimates the cost of the cheapest path from n to the goal
    uchar g_cost;                   // g(n) is the cost of the path from the start node to n,
    struct ASTAR_NODE xdata * came_from;
    struct ASTAR_NODE xdata * next;
} ASTAR_NODE;

typedef struct ASTAR_MAP
{
    long border_x[MAX_BORDER_COUNT];    // 区域划分X边界 ?边界x的实际位置
    long border_y[MAX_BORDER_COUNT];    // 区域划分Y边界
    uchar x_node_count;                 // X方块数量
    uchar y_node_count;                 // Y方块数量
    ASTAR_NODE node[MAX_NODE_COUNT][MAX_NODE_COUNT];
    ASTAR_NODE xdata * open;            // open list
    ASTAR_NODE xdata * closed;          // closed list
} ASTAR_MAP;

typedef struct ASTAR_PATH
{
    long path_x[MAX_PATH_NODE];         // 绕过障碍需要走的中间点坐标
    long path_y[MAX_PATH_NODE];
    uchar path_count;                   // 路径点数
    uchar path_index;                   // 当前正在运行的点索引
} ASTAR_PATH;

extern ASTAR_MAP xdata astar_map;
extern ASTAR_PATH xdata astar_path;

bit Astar_Find_Path(long start_x, long start_y, long end_x, long end_y);
void Astar_Build_Map();
bit Astar_Is_Wall(long x, long y);

#endif // ASTAR_H
